package com.yjhb.android.sbz.manager.im;

import com.yjhb.android.sbz.App;
import com.yjhb.android.sbz.http.HttpObserver;
import com.yjhb.android.sbz.http.HttpTool;
import com.yjhb.android.sbz.mvp.ConventionPresenter;
import com.jojo.android.zxlib.tool.Global;
import com.jojo.android.zxlib.tool.ZxLogTool;
import com.jojo.android.zxlib.tool.ZxToast;
import com.tencent.imsdk.v2.V2TIMManager;
import com.tencent.qcloud.tim.uikit.TUIKit;
import com.tencent.qcloud.tim.uikit.base.IUIKitCallBack;
import com.tencent.qcloud.tim.uikit.modules.conversation.ConversationManagerKit;

/**
 * 创建者 zx
 * 创建时间 2020/12/23 14:54
 */
public class IMManager implements IMView {
    private final String TAG = "IMManager";

    private static IMManager instance;

    public static IMManager getInstance() {
        if (instance == null) {
            instance = new IMManager();
        }
        return instance;
    }


    private IMPresenter imPresenter;

    private IMManager() {
        imPresenter = new IMPresenter(this);
    }

    /**
     * 是否登录
     *
     * @return
     */
    public boolean isLogin() {
        return V2TIMManager.getInstance().getLoginStatus() != V2TIMManager.V2TIM_STATUS_LOGOUT;
    }

    /**
     * 登录
     */
    public void login() {
        if (App.userInfo == null) return;
        if (!isLogin()) {
            imPresenter.getSign();
        }
    }

    /**
     * 下线
     */
    public void logout() {
        if (isLogin()) {
            TUIKit.logout(new IUIKitCallBack() {
                @Override
                public void onSuccess(Object data) {

                }

                @Override
                public void onError(String module, int errCode, String errMsg) {

                }
            });
        }
    }



    @Override
    public void onFailure(int code, String msg) {
        ZxLogTool.e("getIMSign", msg);
        if (code != -1) {
            ZxToast.showINFOBottomToast("消息中心启动失败：" + msg);
        }
    }

    @Override
    public void onSign(String sign) {
        TUIKit.login(App.userInfo.getId(), sign, new IUIKitCallBack() {
            @Override
            public void onSuccess(Object data) {
                ZxLogTool.i(data);
                ConversationManagerKit.getInstance().loadConversation(null);
            }

            @Override
            public void onError(String module, int errCode, String errMsg) {
                ZxLogTool.i("module:" + module + "\n" +
                        "errCode:" + errCode + "\n" +
                        "errMsg:" + errMsg);
                Global.getInstance().getMainHandler().post(new Runnable() {
                    @Override
                    public void run() {
                        ZxToast.showINFOBottomToast("消息中心启动失败：" + errMsg);
                    }
                });
            }
        });
    }


    private class IMPresenter extends ConventionPresenter<IMView> {

        public IMPresenter(IMView imView) {
            super(imView);
        }

        @Override
        protected void onDestroy() {

        }

        void getSign() {
            HttpTool.request(model.getIMSign(), new HttpObserver<String>() {
                @Override
                public void onResult(String data) {
                    if (view != null) {
                        view.onSign(data);
                    }
                }

                @Override
                public void onError(int code, String msg) {
                    if (view != null) {
                        view.onFailure(code, msg);
                    }
                }
            });
        }
    }

}
